<template>
  <header class="header">
    <div class="container">
      <div class="header-content">
        <div class="platform-info" @click="goToHome">
          <img src="../assets/logo.png" alt="Logo" class="header-logo" />
          <h1 class="platform-title">工业电商平台</h1>
          <p class="platform-subtitle">专业机械工业品采购平台</p>
        </div>

        <div class="search-box">
          <input
              type="text"
              class="search-input"
              placeholder="搜索商品..."
              v-model="searchQuery"
              @keyup.enter="performSearch"
          />
          <button class="search-button" @click="performSearch">
            <svg class="search-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <circle cx="11" cy="11" r="8"/>
              <path d="m21 21-4.35-4.35"/>
            </svg>
          </button>
        </div>

        <div class="user-actions">
          <div v-if="!isLoggedIn" class="auth-links">
            <router-link to="/login" class="btn btn-ghost">登录</router-link>
            <router-link to="/register" class="btn btn-primary">注册</router-link>
          </div>
          <div v-else class="user-actions-logged">
            <div class="action-icons">
              <router-link to="/cart" class="icon-link" title="购物车">
                <svg class="icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                  <path d="M9 22a1 1 0 1 0 0-2 1 1 0 0 0 0 2z"/>
                  <path d="M20 22a1 1 0 1 0 0-2 1 1 0 0 0 0 2z"/>
                  <path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"/>
                </svg>
                <span v-if="cartTotalQuantity > 0" class="cart-badge">{{ cartTotalQuantity }}</span>
              </router-link>
              <router-link to="/orders" class="icon-link" title="我的订单">
                <svg class="icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                  <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/>
                  <polyline points="14,2 14,8 20,8"/>
                  <line x1="16" y1="13" x2="8" y2="13"/>
                  <line x1="16" y1="17" x2="8" y2="17"/>
                  <polyline points="10,9 9,9 8,9"/>
                </svg>
              </router-link>
              <router-link to="/address" class="icon-link" title="我的地址">
                <svg class="icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                  <path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z"/>
                  <circle cx="12" cy="10" r="3"/>
                </svg>
              </router-link>
            </div>

            <div class="user-dropdown">
              <button class="user-button" @click="toggleDropdown">
                <span class="user-name">{{ currentUser.username }}</span>
                <svg class="dropdown-icon" :class="{ 'rotate': showDropdown }" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                  <polyline points="6,9 12,15 18,9"/>
                </svg>
              </button>
              <div v-show="showDropdown" class="dropdown-menu">
                <button @click="goToProfile" class="dropdown-item">
                  <svg class="dropdown-item-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/>
                    <circle cx="12" cy="7" r="4"/>
                  </svg>
                  个人中心
                </button>
                <button v-if="isAdmin" @click="goToAdmin" class="dropdown-item admin-item">
                  <svg class="dropdown-item-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <circle cx="12" cy="12" r="3"/>
                    <path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1 1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z"/>
                  </svg>
                  后台管理
                </button>
                <button @click="logout" class="dropdown-item">
                  <svg class="dropdown-item-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/>
                    <polyline points="16,17 21,12 16,7"/>
                    <line x1="21" y1="12" x2="9" y2="12"/>
                  </svg>
                  退出登录
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </header>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue';
import { useRouter } from 'vue-router';
import { useStore } from 'vuex';

// 响应式数据
const showDropdown = ref(false);
const searchQuery = ref('');

// 获取router和store
const router = useRouter();
const store = useStore();

// 计算属性
const currentUser = computed(() => store.state.currentUser);
const isLoggedIn = computed(() => currentUser.value !== null);
const cartTotalQuantity = computed(() => store.getters.cartTotalQuantity);
const isAdmin = computed(() => currentUser.value && currentUser.value.isAdmin === 1);

// 方法
const toggleDropdown = () => {
  showDropdown.value = !showDropdown.value;
};

const goToProfile = () => {
  router.push('/profile');
  showDropdown.value = false;
};

const goToAdmin = () => {
  router.push('/admin');
  showDropdown.value = false;
};
const logout = () => {
  localStorage.removeItem('user');
  showDropdown.value = false;
  // 清空store中的用户和购物车数据
  store.dispatch('logout');
  router.push('/login');
};

const closeDropdown = (event) => {
  // 这里需要获取DOM元素，在Vue 3中需要使用ref
  const headerEl = document.querySelector('.header');
  if (headerEl && !headerEl.contains(event.target)) {
    showDropdown.value = false;
  }
};

const goToHome = () => {
  router.push('/home');
};

const performSearch = () => {
  if (searchQuery.value.trim()) {
    router.push({
      path: '/search',
      query: { q: searchQuery.value }
    });
    searchQuery.value = '';
  }
};

// 生命周期
onMounted(() => {
  document.addEventListener('click', closeDropdown);
  // 如果用户已登录，获取购物车数据
  if (currentUser.value) {
    store.dispatch('fetchCartItems');
  }
});

onUnmounted(() => {
  document.removeEventListener('click', closeDropdown);
});
</script>

<style scoped>
.header {
  background-color: var(--white);
  border-bottom: 1px solid var(--gray-200);
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow-sm);
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--spacing-4) 0;
  gap: var(--spacing-6);
}

.platform-info {
  cursor: pointer;
  transition: opacity var(--transition-fast);
}

.platform-info:hover {
  opacity: 0.8;
}

.platform-title {
  font-size: var(--font-size-2xl);
  font-weight: var(--font-weight-bold);
  color: var(--gray-900);
  margin: 0 0 var(--spacing-1) 0;
  line-height: var(--line-height-tight);
}

.platform-subtitle {
  font-size: var(--font-size-sm);
  color: var(--gray-600);
  margin: 0;
  font-weight: var(--font-weight-normal);
}

.search-box {
  flex: 1;
  max-width: 500px;
  display: flex;
  align-items: center;
  background-color: var(--gray-50);
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-lg);
  overflow: hidden;
  transition: all var(--transition-fast);
}

.search-box:focus-within {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px var(--primary-light);
}

.search-input {
  flex: 1;
  padding: var(--spacing-3) var(--spacing-4);
  border: none;
  background: transparent;
  font-size: var(--font-size-sm);
  color: var(--gray-800);
  outline: none;
}

.search-input::placeholder {
  color: var(--gray-400);
}

.search-button {
  padding: var(--spacing-3) var(--spacing-4);
  background-color: var(--primary-color);
  color: var(--white);
  border: none;
  cursor: pointer;
  transition: background-color var(--transition-fast);
  display: flex;
  align-items: center;
  justify-content: center;
}

.search-button:hover {
  background-color: var(--primary-hover);
}

.search-icon {
  width: 18px;
  height: 18px;
}

.user-actions {
  display: flex;
  align-items: center;
  gap: var(--spacing-4);
}

.auth-links {
  display: flex;
  align-items: center;
  gap: var(--spacing-3);
}

.user-actions-logged {
  display: flex;
  align-items: center;
  gap: var(--spacing-4);
}

.action-icons {
  display: flex;
  gap: var(--spacing-2);
}

.icon-link {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-full);
  transition: all var(--transition-fast);
  position: relative;
  color: var(--gray-600);
}

.icon-link:hover {
  background-color: var(--gray-100);
  color: var(--primary-color);
  transform: translateY(-1px);
}

.icon {
  width: 20px;
  height: 20px;
  stroke: currentColor;
  fill: none;
  transition: all var(--transition-fast);
}

.cart-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  background-color: var(--error-color);
  color: var(--white);
  border-radius: var(--radius-full);
  width: 18px;
  height: 18px;
  font-size: var(--font-size-xs);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: var(--font-weight-bold);
  min-width: 18px;
  border: 2px solid var(--white);
}

.user-dropdown {
  position: relative;
}

.user-button {
  background: none;
  border: none;
  color: var(--gray-700);
  font-size: var(--font-size-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: var(--spacing-2);
  padding: var(--spacing-2) var(--spacing-3);
  border-radius: var(--radius-md);
  transition: all var(--transition-fast);
  font-weight: var(--font-weight-medium);
}

.user-button:hover {
  background-color: var(--gray-100);
  color: var(--gray-900);
}

.user-name {
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dropdown-icon {
  width: 16px;
  height: 16px;
  stroke: currentColor;
  fill: none;
  transition: transform var(--transition-fast);
}

.dropdown-icon.rotate {
  transform: rotate(180deg);
}

.dropdown-menu {
  position: absolute;
  right: 0;
  top: calc(100% + var(--spacing-2));
  background-color: var(--white);
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  min-width: 160px;
  z-index: 100;
  overflow: hidden;
}

.dropdown-item {
  width: 100%;
  padding: var(--spacing-3) var(--spacing-4);
  text-align: left;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--gray-700);
  font-size: var(--font-size-sm);
  display: flex;
  align-items: center;
  gap: var(--spacing-3);
  transition: all var(--transition-fast);
  border-bottom: 1px solid var(--gray-100);
}

.dropdown-item:last-child {
  border-bottom: none;
}

.dropdown-item:hover {
  background-color: var(--gray-50);
  color: var(--gray-900);
}

.dropdown-item-icon {
  width: 16px;
  height: 16px;
  stroke: currentColor;
  fill: none;
  flex-shrink: 0;
}

.admin-item {
  color: var(--primary-color);
  font-weight: var(--font-weight-medium);
}

.admin-item:hover {
  background-color: var(--primary-light);
  color: var(--primary-hover);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-content {
    flex-direction: column;
    gap: var(--spacing-4);
    padding: var(--spacing-3) 0;
  }
  
  .platform-title {
    font-size: var(--font-size-xl);
  }
  
  .search-box {
    max-width: 100%;
  }
  
  .user-actions {
    width: 100%;
    justify-content: center;
  }
  
  .action-icons {
    gap: var(--spacing-1);
  }
  
  .icon-link {
    width: 36px;
    height: 36px;
  }
}

.header-logo {
  height: 48px;
  margin-right: 12px;
  vertical-align: middle;
}
</style>